package com.lntea.context;

import java.util.Map.Entry;

import com.lntea.beans.BeanDefinition;
import com.lntea.factory.AbstractBeanFactory;
import com.lntea.factory.AutowireCapableBeanFactory;
import com.lntea.io.ResourceLoader;
import com.lntea.xml.XmlBeanDefinitionReader;

public class ClasspathXmlApplicationContext extends AbstractApplicationContext {
	private String location;
	
	public ClasspathXmlApplicationContext(String location) {
		this(location,new AutowireCapableBeanFactory());
	}

	public ClasspathXmlApplicationContext(String location,AbstractBeanFactory beanFactory) {
		super(beanFactory);
		this.location = location;
		try {
			refresh();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void loadBeanDefinitions(AbstractBeanFactory beanFactory) throws Exception {
		XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(new ResourceLoader());
		reader.loadBeanDefinitions(location);
		
		for(Entry<String, BeanDefinition> entry : reader.getRegistry().entrySet()){
			beanFactory.registerBeanDefinition(entry.getKey(), entry.getValue());
		}
	}

}
